<?php
/**
 * 部门架构
 * @author eddietian
 * **/
apf_require_controller('Oa_Attendance_Abstract');
apf_require_class('Bll_Attendance_ExportDepartment');

class Oa_Attendance_DepartmentTreeController extends Oa_Attendance_AbstractController {

    private $result = array();
    public function handle_request() {
        ini_set("memory_limit",'512M');
        set_time_limit(0);
        $data = array();

        $root_user_id = 6932;
        $this->bll_department = new Bll_Attendance_ExportDepartment();
        $data = $this->bll_department->find_tree_by_user($root_user_id);
//print_r($data);
        $data = $this->format_tree($data);

        $this->request->set_attribute('title',"部门树形结构");
        $this->request->set_attribute('data',$data);

        return 'Oa_Attendance_Position';
    }

    public function format_tree($data) {
        $this->build_data($data);
        $s = "";
        foreach($this->result as $dname => $v) {
            $vname = $this->get_dname($dname,$v['bu_name']);
            $s .= "<li><span>{$vname}&nbsp;{$this->get_arr_name($v['user_name'])}</span>";
            $s .= $this->get_child_tree($v);
            $s .= "</li>";
            $this->result[$dname] = "";
            unset($this->result[$dname]);
        }
        return $s;
    }
    private function dep_list(){
        $s_sql = 'SELECT * from department where 1';
        $rs = $this->bll_department->get_default_dao()->find_by_sql($s_sql);
        $deps = array();
        foreach($rs as $val){
            $deps[$val['department_id']]=$val;
        }
        $sql2="select user_id,user_name,position_name,function_id,bu_id,department_id,work_city_id from dep_user where job_status=1";
        $all_user = $this->bll_department->get_default_dao()->find_by_sql($sql2);
        $la=array();
        foreach ($all_user as $val){
            if($val['work_city_id']!=11){continue;}
            $la[$val['department_id']][] = $val;
        }
        $last = array();
        foreach($this->result as $dname=>$v){
            $st=$dname.",";
            foreach($v as $k=> $val){

                $fist = array_shift($val);
                $last[$dname][$k]['manage']=$fist;
                $last[$dname][$k]['users']=$la[$fist['department_id']];
            }

        }
        foreach($last as $dname=>$v){
            echo $dname."\n";
            foreach($v as $k=>$val){
                echo ",".$k."(".$val['manage']['user_name'].")\n";
                $s=array();
                foreach($val['users'] as $valx){
                    $s[] =$valx['user_name'];
                }
                echo ",,".implode(',',$s)."\n";
            }
        }
    }
    private function build_data($data,$dname = "") {
        if(empty($data)) {return array();}
        $result = array();
        foreach($data as $id => $info) {
            if(empty($info['children'])) {continue;}
            $tmp = array();
            $tmp['user_id'] = $info['user_id'];
            $tmp['user_name'] = $info['user_name'];
            $tmp['parent_id'] = $info['parent_id'];
            $tmp['department_name'] = $info['department_name'];
            $tmp['bu_name'] = $info['bu_name'];
            $tmp['city_name'] = $info['city_name'];
            $tmp['department_id'] = $info['department_id'];
            if($dname) {
                if($dname == $info['department_name']) {
                    $this->result[$info['department_name']][$tmp['bu_name']][$id] = $tmp;
                }else {
                    $this->result[$info['department_name']][$tmp['bu_name']][$id] = $tmp;
                }
            }else {
                $this->result[$info['department_name']][$tmp['bu_name']][$id] = $tmp;
            }
            $this->build_data($info['children'],$tmp['department_name']);
            unset($data[$id]);
        }
        return $result;
    }

    private function get_child_tree($arr) {
        if(empty($arr)){return "";}
        $s = "";

        foreach($arr as $dname => $v) {
            //if($dname == 'children') {continue;}
            $vname = $this->get_dname($dname,$v['city_name'],$v['bu_name']);
            $s .= "<li><span>{$vname}&nbsp;{$this->get_arr_name($v['user_name'])}</span>";
            $s .= $this->get_child_tree1($v);
            $s .= "</li>";
            $arr[$dname] = "";
            unset($arr[$dname]);
        }
        return "<ul>".$s."</ul>";
    }

    private function get_child_tree1($arr) {
        if(empty($arr)){return "";}
        $s = "";

        foreach($arr as $dname => $v) {
            //if($dname == 'children') {continue;}
            $vname = $this->get_dname($v['city_name']);
            $s .= "<li><span>{$vname}&nbsp;{$this->get_arr_name($v['user_name'])}</span>";
            $s .= "</li>";
            $arr[$dname] = "";
            unset($arr[$dname]);
        }
        return "<ul>".$s."</ul>";
    }

    public function get_dname($cname,$bname,$dname) {
        $result = array();
        if(!empty($cname)) {
            $result[] = $cname;
        }
        if(!empty($bname)) {
            $result[] = $bname;
        }
        if(!empty($dname)) {
            $result[] = $dname;
        }
        return implode("/",$result);
    }

    private function get_status($s) {
        return "";
        if($s == '2') {
            return "&nbsp;<i>兼职</i>";
        }
        return "";
    }

    private function get_arr_name($v) {
        if(empty($v)) {return "";}
        return '['.$v.']';
    }
}
